/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.truonghau.smstoxls.service;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.

import java.util.Calendar;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

import com.truonghau.xmeasure.commons.Constants;
import com.truonghau.xmeasure.commons.SmSUtils;

/**
 * This is an example of implementing an application service that will run in
 * response to an alarm, allowing us to move long duration work out of an
 * intent receiver.
 * 
 * @see AlarmService
 * @see AlarmService_Alarm
 */
public class AlarmService_Service extends Service {
	private final String NOTIF_18H ="Yêu cầu nhân viên gửi sms doanh số!";
	private NotificationManager mNM;
	@Override
	public void onCreate() {
		//15' caller interval
		Calendar cal = Calendar.getInstance();
		int h18 = cal.get(Calendar.HOUR_OF_DAY); 
		if(h18 == 17){
			if(SmSUtils.checkForNotif(this)){
				SharedPreferences settings = getSharedPreferences(
						Constants.PREFS_NAME, 0);
				String txtMsgNotifStr = settings.getString(Constants.TxtMsgNotif, "");
				SmSUtils.sendSMS(this, txtMsgNotifStr);
				//for test
				mNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
				SmSUtils.showNotification(this, mNM, NOTIF_18H);
			}

		}
		Thread thr = new Thread(null, mTask, "AlarmService_Service");
		thr.start();
	}
	/**
	 * The function that runs in our worker thread
	 */
	Runnable mTask = new Runnable() {
		public void run() {
			long endTime = System.currentTimeMillis() + 15*1000;
			while (System.currentTimeMillis() < endTime) {
				synchronized (mBinder) {
					try {
						mBinder.wait(endTime - System.currentTimeMillis());
					} catch (Exception e) {
					}
				}
			}

			// Done with our work...  stop the service!
			AlarmService_Service.this.stopSelf();
		}
	};
	@Override
	public void onDestroy() {

		// Tell the user we stopped.
		// Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();
	}

	/**
	 * The function that runs in our worker thread
	 */

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}


	/**
	 * This is the object that receives interactions from clients.  See RemoteService
	 * for a more complete example.
	 */
	private final IBinder mBinder = new Binder() {
		@Override
		protected boolean onTransact(int code, Parcel data, Parcel reply,
				int flags) throws RemoteException {
			return super.onTransact(code, data, reply, flags);
		}
	};


}

